<?php

class DB {
    
    public static $config = array();
    
    public static function connect()
    {
	
	$config = require 'config/db.php';
	if (!class_exists('PDO', false))
	    throw new Exception("PHP PDO package is required.");

	if (empty($config))
	    throw new Exception("Database definitions required.");

	$dsn = "{$config['driver']}:host={$config['host']};dbname={$config['database']};charset={$config['charset']}";
	try
	{
	    $db_handle = new PDO(
			    $dsn,
			    $config['username'],
			    $config['password'],
			    array(PDO::ATTR_PERSISTENT => !empty($config['persistent']) ? true : false)
	    );
	    $db_handle->exec("SET CHARACTER SET {$config['charset']}");
	}
	catch (PDOException $e)
	{
	    throw new Exception(sprintf("Can't connect to PDO database '{$config['type']}'. Error: %s", $e->getMessage()));
	}

	$db_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	return $db_handle;
    }
    
    public static function get_config()
    {
	require 'config/db.php';
	DB::$config = $db;
    }
    
}